<?php
/*
 * 推荐位内容管理model
 * @auth suosuo
 */
namespace Common\Model;
use Think\Model;

class PositionContentModel extends Model{
	private $_db = '';
	
	public function __construct (){
		$this->_db = M('position_content');
	}
	
	public function insert ($data){
		if(!is_array($data) || !$data){
			return 0;
		}
		$data['create_time'] = time();
		return $this->_db->add($data);
	}
	
	public function getPositionContent ($data,$page,$pageSize) {
		$conditions = $data;
		if(isset($data['title']) && $data['title']){
			$conditions['title'] = array ('like','%'.$data['title'].'%');
		}
		if(isset($data['position_id']) && $data['position_id']){
			$conditions['position_id'] = intval($data['position_id']);
		}
		$offset = ($page - 1) * $pageSize;
		$list = $this->_db->where($conditions)
		->order('listorder desc,id desc')
		->limit($offset,$pageSize)
		->select();
		return $list;
		
	}
	
	public function getPositionContentCount ($data) {
		$conditions = $data;
		if(isset($data['title']) && $data['title']){
			$conditions['title'] = array ('like','%'.$data['title'].'%');
		}
		if(isset($data['position_id']) && $data['position_id']){
			$conditions['position_id'] = intval($data['position_id']);
		}
		return $this->_db->where($conditions)->count();
	}
	
	public function updataStatusById($id,$status){
		if(!is_numeric($id) || !$id){
			throw_exception("ID不合法");
		}
		if(!is_numeric($status) || !$status){
			throw_exception("状态不合法");
		}
		$data['status']=$status;
		return $this->_db->where('id='.$id)->save($data);
	}
	
	public function updataListorderById ($id,$listorder){
		if(!$id || !is_numeric($id)){
			throw_exception("ID不合法");
		}
		$data=array(
			'listorder'=>intval($listorder),
		);
		return $this->_db->where('id='.$id)->save($data);
	}
	
	public function getPositionConById($id) {
		if(!is_numeric($id) || !$id){
			throw_exception("ID不合法");
		}
		$data = $this ->_db->where('id='.$id)->find();
		return $data;
	}
	
	public function updataPositionConById ($id,$data){
		if( !$id || !is_numeric($id) ){
			throw_exception('ID不合法');
		}
		if( !$data || !is_array($data) ){
			throw_exception('更新的数据不合法');
		}
		$data['update_time']=time();
		return $this->_db->where('id='.$id)->save($data);
	}
	
	public function select ($data,$pageSize) {//前端获取推荐位内容
		$conditions = $data;
		if(isset($data['title']) && $data['title']){
			$conditions['title'] = array ('like','%'.$data['title'].'%');
		}
		if(isset($data['position_id']) && $data['position_id']){
			$conditions['position_id'] = intval($data['position_id']);
		}
		
		$list = $this->_db->where($conditions)
		->order('listorder desc,id desc')
		->limit(0,$pageSize)
		->select();
		return $list;
		
	}
}